<?php
echo CHtml::script("var currentProductId=0;
	    function messagebox(message,iserror){
			$('#messagebox').html(message);
			if(iserror) $('#messagebox').css('color','red'); else $('#messagebox').css('color','green');
	        $('#messagebox').fadeTo(\"slow\", 1).animate({opacity: 1.0}, 3000).fadeTo(\"slow\", 0);
	    }
	function preparedel(id,name){
	document.getElementById(\"delproductid\").value = id;
	document.getElementById(\"delproductname\").innerHTML = name;
	$(\"#delproddialog\").dialog(\"open\"); 
	return false;
}");

$this->renderPartial("period",array("startdate"=>$startdate,"enddate"=>$enddate));

$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>'delproddialog',
    // additional javascript options for the dialog plugin
    'options'=>array(
        'title'=>'Delete product',
        'autoOpen'=>false,
		'width'=>600
    ),
));
echo CHtml::beginForm();
?>
<table>
	<tr><td>Продукт:</td><td id="delproductname"></td></tr>
</table>
<?php
echo CHtml::hiddenField("delproductid",0,array("id"=>"delproductid"));
echo CHtml::ajaxSubmitButton('Delete',CHtml::normalizeUrl(array('store/deletesold',"ajax"=>1)),array('success'=>'js: function(data) 
	{
		var row=$.fn.yiiGridView.getRowByKey("yw0",currentProductId);
		var cnt=0; 
		row[7].innerHTML=cnt;
		row[8].innerHTML=cnt;
		row[8].innerHTML="";
		$("#delproddialog").dialog("close");
	}'),array('id'=>'delproductbutton','name'=>'delproductbutton'));
echo CHtml::button('Close',array('onclick'=>'$(\'#delproddialog\').dialog("close")'));
echo CHtml::endForm();
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
<?php
$provider = $model->advanced_search($startdate,$enddate,true);
$mydata = $model->advanced_search($startdate,$enddate,false);
$totals = $model->getTotals($mydata);
$columns = array(
		array(
			'value'=>'$data->name',
			'header'=>'Продукт',
			'name'=>'name'
		),
		array(
			'value'=>'$data->firm->name',
			'header'=>'Фірма',
			'name'=>'firmid',
			'filter'=>CHtml::dropDownList("Products[firmid]",$model->firmid,$dataForFirmsDropdown)
		),
		array(
			'value'=>'$data->billprice',
			'header'=>'Закупка',
			'name'=>'billprice',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sumbillprice'],
		),
		array(
			'value'=>'$data->price',
			'header'=>'Ціна',
			'name'=>'price',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sumprice'],
		),
		array(
			'value'=>'$data->unsoldcount',
			'header'=>'Кількість',
			'name'=>'unsoldcount',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sumcount'],
		),
		array(
			'value'=>'$data->bought',
			'header'=>'Куплено',
			'name'=>'bought',
			'footer'=>$provider->itemCount===0 ? '' : $totals['count'],
		),
		array(
			'value'=>'$data->sumbought',
			'header'=>'Сума',
			'name'=>'sumbought',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sum'],
		),
		array(
			'value'=>'$data->sold',
			'header'=>'Продано',
			'name'=>'sold',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sold'],
		),
		array(
			'value'=>'$data->sumsold',
			'header'=>'Сума',
			'name'=>'sumsold',
			'footer'=>$provider->itemCount===0 ? '' : $totals['sumsold'],
		)
	);
if(isset($startdate) && isset($enddate) && $startdate==$enddate)
{
	$columns[] = array(
			'value'=>'($data->sold>0)? CHtml::link(CHtml::image("images/minus16x16.png"),array("#"),array("onclick"=>"currentProductId=$data->id;preparedel($data->id,\"".addslashes($data->name)."\");\$(\"#delproddialog\").dialog(\"open\");return false;")):""',
			'type'=>'raw'
		);
}
$this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider'=>$provider,
	'columns'=>$columns,
	'filterPosition'=>'body',
	'filter'=>$model
)); 
?>
<div id="messagebox"></div>